//
//  CZDetailHeaderView.swift
//  GZWeibo05
//
//  Created by zhangping on 15/11/19.
//  Copyright © 2015年 zhangping. All rights reserved.
//

import UIKit

// MARK: - 头部选中按钮的类型
/// 头部选中按钮的类型
enum CZDetailHeaderViewSelectedButtonType: Int {
    case Repost = 0
    case Comment = 1
    case Lick = 2
}

// MARK: - 按钮点击代理
/// 按钮点击代理
protocol CZDetailHeaderViewDelegate: NSObjectProtocol {
    
    /**
    headerView被点击了
    - parameter view:              headerView
    - parameter didSelectedButton: 点击了的类型
    */
    func detailHeaderView(view: UIView, didSelectedButton: CZDetailHeaderViewSelectedButtonType)
}

class CZDetailHeaderView: UIView {
    
    // MARK: - 属性
    /// 代理
    weak var delegate: CZDetailHeaderViewDelegate?
    
    /// 微博模型
    var status: CZStatus? {
        didSet {
            setButton(forwardButton, title: "转发", count: status!.reposts_count)
            setButton(commentButton, title: "评论", count: status!.comments_count)
            setButton(lickButton, title: "赞", count: status!.attitudes_count)
        }
    }
    
    /// 选中按钮
    private var selectedButton: UIButton?
    
    /// 当前选中的类型,默认是评论
    var selectedButtonType: CZDetailHeaderViewSelectedButtonType? = CZDetailHeaderViewSelectedButtonType.Comment
    
    /// 三角形按钮CenterX约束
    private var hintCenterXCon: NSLayoutConstraint?
    
    // MARK: - 构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 设置背景颜色
        backgroundColor = UIColor(white: 0.99, alpha: 1)
        
        prepareUI()
    }

    // MARK: - 设置按钮文字
    /**
    设置按钮文字
    - parameter button: 要设置文字的按钮
    - parameter title:  标题
    - parameter count:  数量
    */
    private func setButton(button: UIButton, title: String, count: Int) {
        if count >= 10000 {
            let final = CGFloat(count) / 10000.0
            var titleText = String(format: "%.1f万", arguments: [final])
            titleText = titleText.stringByReplacingOccurrencesOfString(".0", withString: "")
            
            button.setTitle(title + " " + titleText, forState: UIControlState.Normal)
        } else {
            button.setTitle("\(title) \(count)", forState: UIControlState.Normal)
        }
    }
    
    // MARK: - 按钮点击事件
    /**
    按钮点击事件
    - parameter button: 被点击的按钮
    */
    func buttonClick(button: UIButton) {
        // 设置选中
        selectedButton?.selected = false
        button.selected = true
        selectedButton = button
        
        // 移动三角形
        hintCenterXCon?.constant = CGRectGetMidX(button.frame)
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
        
        var type = CZDetailHeaderViewSelectedButtonType.Repost
        
        if button == commentButton {
           type = CZDetailHeaderViewSelectedButtonType.Comment
        } else if button == lickButton {
           type = CZDetailHeaderViewSelectedButtonType.Lick
        }
        
        selectedButtonType = type
        // 通知代理
        delegate?.detailHeaderView(self, didSelectedButton: type)
    }
    
    // MARK: - 准备UI
    private func prepareUI() {
        // 添加点击事件
        forwardButton.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        commentButton.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        lickButton.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 添加子控件
        addSubview(forwardButton)
        addSubview(commentButton)
        addSubview(lickButton)
        addSubview(separatorViewOne)
        addSubview(hintView)
        
        // 添加约束
        
        // 转发按钮
        forwardButton.ff_AlignInner(type: ff_AlignType.CenterLeft, referView: self, size: CGSize(width: 70, height: 44))
        
        // 评论按钮
        commentButton.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: forwardButton, size: CGSize(width: 70, height: 44))
        
        // 赞按钮
        lickButton.ff_AlignInner(type: ff_AlignType.CenterRight, referView: self, size: CGSize(width: 70, height: 44), offset: CGPoint(x: 0, y: 0))
        
        // 分割线
        separatorViewOne.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: forwardButton, size: nil)
        
        // 三角型
        hintView.translatesAutoresizingMaskIntoConstraints = false
        hintCenterXCon = NSLayoutConstraint(item: hintView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 105)
        addConstraint(hintCenterXCon!)
        addConstraint(NSLayoutConstraint(item: hintView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }
    
    // MARK: - 懒加载
    /// 转发
    private lazy var forwardButton: UIButton = UIButton(title: "转发", titleColor: UIColor.lightGrayColor(), selectedTitleColor: UIColor.darkGrayColor(), fontSzie: 11)
    
    /// 评论
    private lazy var commentButton: UIButton = UIButton(title: "评论", titleColor: UIColor.lightGrayColor(), selectedTitleColor: UIColor.darkGrayColor(), fontSzie: 11)
    
    /// 赞
    private lazy var lickButton: UIButton = UIButton(title: "赞", titleColor: UIColor.lightGrayColor(), selectedTitleColor: UIColor.darkGrayColor(), fontSzie: 11)
    
    /// 水平分割线1
    private lazy var separatorViewOne: UIImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line_highlighted"))
    
    /// 三角形
    private lazy var hintView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "statusdetail_segmented_bottom_arrow"))
        
        return imageView
    }()
}
